import { request } from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/category/category.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        leftMenuList: [],
        rightContent: [],
        currentIndex: 0,
        scrollTop:0
    },
    cateList: [],

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const Cates = wx.getStorageSync('cates');
        if (!Cates) {
            this.getCate()
        } else {
            if (Date.now() - Cates.time > 1000 * 10) {
                this.getCate()
            } else {
                this.cateList = Cates.data
                let leftMenuList = this.cateList.map(v => v.cat_name)
                let rightContent = this.cateList[0].children
                this.setData({
                    leftMenuList,
                    rightContent
                })
            }
        }

    },
    /* 获取分类数据 */
    /* getCate() {
        request({ url: "/categories"})
            .then(res => {
                this.cateList = res.data.message
                wx.setStorageSync('cates', { time: Date.now(), data: this.cateList })
                let leftMenuList = this.cateList.map(v => v.cat_name)
                let rightContent = this.cateList[0].children
                this.setData({
                    leftMenuList,
                    rightContent,
                })
            })
    }, */
    async getCate() {
        const res = await request({ url: "/categories"})
        this.cateList = res
        wx.setStorageSync('cates', { time: Date.now(), data: this.cateList })
        let leftMenuList = this.cateList.map(v => v.cat_name)
        let rightContent = this.cateList[0].children
        this.setData({
            leftMenuList,
            rightContent,
        })
    },
    /* 左侧菜单点击事件 */
    handleItemTap(e) {
        /* 获取被点击标题的索引 */
        const { index } = e.currentTarget.dataset
        let rightContent = this.cateList[index].children
        /* 给data中的currentIndex赋值就可以了 */
        this.setData({
            currentIndex: index,
            rightContent,
            /* 重置顶部距离 */
            scrollTop:0
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})